package com.ts.main.admin.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ts.main.admin.dto.ProductOptionDto;
import com.ts.main.admin.dto.ProductOptionSearchPageDto;
import com.ts.main.admin.entity.ProductOptionEntity;
import com.ts.main.admin.exception.RPage;
import com.ts.main.admin.mapper.ProductOptionMapper;
import com.ts.main.admin.vo.*;
import jakarta.annotation.Resource;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author lk
 * @description 针对表【t_product_option】的数据库操作Service实现
 * @createDate 2025-06-09 00:56:13
 */
@AllArgsConstructor
@Slf4j
@Service
public class ProductOptionServiceImpl extends ServiceImpl<ProductOptionMapper, ProductOptionEntity> {


    private final ProductOptionValueServiceImpl productOptionValueService;

    private final ProductServiceImpl productService;


    public RPage<List<ProductOptionVo>> getProductOptionPage(ProductOptionSearchPageDto dto) {
        Page<Object> page = PageHelper.startPage(dto.getPage(), dto.getLimit());
        List<ProductOptionVo> list = baseMapper.getProductOptionPage(dto);
        return RPage.ok(list, dto.getPage(), dto.getLimit(), page.getTotal());
    }

    public List<ProductOptionVo> getProductOptionList(Integer productId) {
         return this.baseMapper.getProductOptionList(productId);
    }


    public ProductOptionVo getProductOptionDetails(Integer productOptionId) {
        return baseMapper.getProductOptionDetails(productOptionId);
    }

    @Transactional
    public Boolean saveProductOption(ProductOptionDto dto) {
        //选项值
        List<String> valueList = dto.getValueList();
        if (!CollUtil.isEmpty(valueList))
            productOptionValueService.saveValue(dto.getOptionId(), valueList);
        return this.baseMapper.saveProductOption(dto);
    }

    @Transactional
    public Boolean updateProductOption(ProductOptionDto dto) {
        updateById(BeanUtil.toBean(dto, ProductOptionEntity.class));
        //选项值
        productOptionValueService.deleteValue(dto.getOptionId());
        if (!CollUtil.isEmpty(dto.getValueList()))
            productOptionValueService.saveValue(dto.getOptionId(), dto.getValueList());
        return true;
    }

    public Boolean deleteProductOption(Integer productOptionId) {
        return removeById(productOptionId);
    }

    public List<ProductPartVo> getProductPartList() {
        return productService.getProductPartList();
    }

}




